﻿using server.Model;
using server.Tool;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace server.Core
{
   public  static class SessionMsg
    {
        /// <summary>
        /// 三十秒超时
        /// </summary>
        private static readonly int  connectTime = 8;
        public   static void RemoveTimeOutConnectSession()
        {
            ConcurrentDictionary<PubgSession, SessionItem> dic = PubgSession.mOnLineConnections;
            Console.WriteLine("在线人数={0}" ,dic.Count);
            foreach (PubgSession session in dic.Keys)
            {
                //Console.WriteLine("目前:"+ session.RemoteEndPoint);
                SessionItem sessionItem = null;
                dic.TryGetValue(session, out sessionItem);
                long currentTime = TimeUtils.GetCurrentTimestamp();

                if (sessionItem != null)
                {
                    if ((sessionItem.heartTimeStamp > 0 && (currentTime - sessionItem.heartTimeStamp > connectTime) ||
                        sessionItem.heartTimeStamp == -1 && (currentTime - sessionItem.createTimeStamp > connectTime)))
                    {
                        dic.TryRemove(session, out sessionItem);
                        session.Close();
                        Console.WriteLine(sessionItem.gpsItem.userName + ":" + "连接超时，客户端被强制中断。");

                    }

                }
            }

            //Console.WriteLine("当前服务器连接数量：" + dic.Count);
        }

    }
}
